<template>
	<view>
		<!-- //地图 -->
		<view class="loading-page" v-if="loading">
			<u-loading mode="circle" size="100"></u-loading>
		</view>
		<view class="main-page" v-else>
			<view class="map-box">
				<view class="page-body">
					<view class="page-section page-section-gap">
						<map @markertap="markertap" scale='12' :markers="markers" style="width: 100%; height:calc(20vh);"
							:latitude="latitude" :longitude="longitude" id="map" ref='map' @callouttap='callouttap'
							show-location='true'>
							<cover-view slot="callout">
								<block v-for="(item,index) in markers" :key="index">
									<cover-view class="customCallout" :marker-id="item.id">
										<cover-view class="content">
											{{item.id}} 内容
										</cover-view>
									</cover-view>
								</block>
							</cover-view>
						</map>
					</view>
				</view>
			</view>
			<!-- <view class="search-box u-p-15">
				<u-search placeholder="搜索站点" v-model="keyword" :clearabled="true" :show-action="false" shape="square"></u-search>
			</view> -->
			
			<!-- 第二种样式 -->
			<view class="map-box-1">
				<view class="u-m-30" v-for="(cell,index) in listdata" :key="cell.id">
					<view class="list-box">
						<view class="list-l">
							<view class="title u-line-1 u-p-b-10">
								<view class="u-font-36 u-line-1">{{cell.name}}</view>
							</view>
							<view class="title-2">
								<u-icon name="map" size="28"></u-icon><text class="u-font-26">{{cell.address.address}}</text>
							</view>
							<view class="title-2">
								<u-icon name="clock" size="28"></u-icon>
								<text class="u-font-26">{{cell.servicetime}}</text>
							</view>
							<!-- <view class="tag">
								<u-tag class="tag-item" v-if="cell.storetype == '自营站点'" :text="cell.storetype" size="mini" type="success" mode="plain"/>
								<u-tag class="tag-item" v-if="cell.storetype !== '自营站点'" :text="cell.storetype" size="mini" mode="plain"/>
							</view> -->
						</view>
						<view class="list-r">
							<view class="distance">
								<text class="d1">距离</text><text class="d2">{{cell.distance}}</text><text class="d3">km</text>
							</view>
							<view class="btn">
								<view class="item" @click="mygomap(cell)">
									<u-icon name="map-fill" color="#10beca" size="32"></u-icon>
								</view>
								<view class="item" @click="callNumber(cell.telephone)">
									<u-icon name="phone-fill" color="#10beca" size="32"></u-icon>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			
			<view class="grid-box">
				<view class="guide">
					<view class="head-title"><text>回收指南</text></view>
					<view class="list-con">
						<view class="item">
							<view class="icon">
								<text class="iconfont icon-huishou"></text>
							</view>
							<text class="name">打包废品</text>
						</view>
						<u-icon name="arrow-right" class="arrow-ico" :color="'#999'"></u-icon>
						<view class="item">
							<view class="icon">
								<text class="iconfont icon-dizhiguanli"></text>
							</view>
							<text class="name">查最近站点</text>
						</view>
						<u-icon name="arrow-right" class="arrow-ico" :color="'#999'"></u-icon>
						<view class="item">
							<view class="icon">
								<text class="iconfont icon-xiaogeshangmen"></text>
							</view>
							<text class="name">送到站点</text>
						</view>
						<u-icon name="arrow-right" class="arrow-ico" :color="'#999'"></u-icon>
						<view class="item">
							<view class="icon">
								<text class="iconfont icon-wanchengjiesuan"></text>
							</view>
							<text class="name">回收赚钱</text>
						</view>
					</view>
				</view>
			</view>
			<view class="button-group-list">
				<view class="g-button u-btn" @click="showTodayPrice = true"><i
						class="iconfont icon-dingdan u-m-r-10"></i><text>今日参考价</text></view>
				<!-- <view class="g-button u-btn u-m-l-15" @click="showHistoryPrice = true"><i class="iconfont icon-feizhi u-m-r-10"></i><text>历史成交价</text></view> -->
			</view>
		</view>
		<!-- 今日参考价 -->
		<u-popup v-model="showTodayPrice" mode="bottom" border-radius="14" z-index="9998" length="80%">
			<view class="u-p-30">
				<text class="u-font-32">今日参考价</text>
				<view class="tips u-m-r-0 u-m-l-0 u-m-b-30">
					<text>回收价格是根据市场变化实时变更，下单后小哥上门对物品进行确认也会对价格产生波动，当前页面价格仅供参考，具体以订单确认价格为准。</text>
				</view>
				<price-table v-if="showTodayPrice == true" @online="online" @shop="goShop"></price-table>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				ProductList: [],
				chooseType: [

				],
				showTodayPrice: false, //今日参考价显示
				showHistoryPrice: false, //历史成交价显示
				current: 1,
				//地图位置参数
				id: 0, // 使用 marker点击事件 需要填写id
				title: 'map',
				checkedWeixin: false,
				markers: [
					// 	{
					// 	id: 0,
					// 	title: "测试1",
					// 	width: 58,
					// 	height: 58,
					// 	latitude: 31.23588,
					// 	longitude: 121.48053,
					// },
				],
				list: [{
						name: '上门回收'
					},
					{
						name: '自送网点'
					}
				],
				listdata: [],
				longitude: 0, //经度
				latitude: 0, //维度
				tabcurrent: 1,
				loading: true
			};
		},
		computed: {
			...mapState({
				webSetting: state => state.webSetting,
				userdetail: state => state.userdetail,
			})
		},
		mounted(opt) {
			this.loading = true
			uni.getLocation({
				type: 'gcj02',
				geocode: true,
				success: res=> {
					console.log(res)
					console.log('当前位置的经度：' + res.longitude);
					console.log('当前位置的纬度：' + res.latitude);
				},
				fail: res=>{
					console.log('getLocation-fail',res)
				},
				complete: res=>{
					
				}
			});
			this.getlist().then(()=>{
				this.loading = false
			})
		},
		methods: {
			online(){
				this.showTodayPrice = false
				this.$emit('tab',0)
			},
			goShop(){
				this.showTodayPrice = false
				this.$emit('tab',1)
			},
			callouttap(e) {
				const that = this
				let markers = this.markers
				markers.find(function(item, index) {
					if (item.id == e.markerId) {
						item.width = 30 * 1.5
						item.height = 30 * 1.5
					} else {
						item.width = 30
						item.height = 30
					}
				})
			},
			mygomap(data) {
				console.log('mygomap',data)
				uni.openLocation({
					latitude: data.address.lat,
					longitude: data.address.lng,
					name: data.address.address,
					// address: '距您直线' + data.distance + '公里',
					success: function() {
						
					}
				});
			},
			callNumber(number){
				uni.makePhoneCall({
					phoneNumber: number
				})
			},
			// go(e) {
			// 	console.log(e)
			// 	if (e == 0) {
			// 		// uni.reLaunch({
			// 		// 	url: "/pages/apply/apply"
			// 		// });
			// 		wx.reportEvent("tap_recovery", {
			// 			"type": this.userdetail.type,
			// 			"mobile": this.userdetail.mobile,
			// 			"usename": this.userdetail.name,
			// 			"role": "用户点击上门回收tap"
			// 		})
			// 		this.$emit('tab',0)
			// 	}
			// },
			markertap(e) {
				const that = this
				let markers = this.markers
				markers.find(function(item, index) {
					if (item.id == e.markerId) {
						// item.width = 30 * 1.5
						// item.height = 30 * 1.5
						console.log(item, 'item=>')
						uni.openLocation({
							latitude: item.latitude,
							longitude: item.longitude,
							name: item.address,
							// address: data.citynames,
							success: function() {
								
							}
						});
					} else {
						// item.width = 30
						// item.height = 30
					}
				})

			},
			// 计算距离函数
			Rad(d) {
				// console.log(d,'这里是D');
				//根据经纬度判断距离
				return d * Math.PI / 180.0;
			},
			//获取当前位置
			getlist() {
				return this.$u.post('/Store/List', {
					PageIndex: 1,
					PageSize: 999,
					isManage: true,
					state: 2, //状态 0全部，1冻结，2启用
				}).then((data) => {
					let arrdata = data.rows
					this.listdata = arrdata

					console.log(this.listdata, 'ssssdsdda')
					let that = this
					uni.getLocation({
						type: 'gcj02',
						success: (res) => {
							console.log('当前位置的经度：' + res.longitude);
							console.log('当前位置的纬度：' + res.latitude);
							console.log('名字：' + res.address);
							that.latitude = res.latitude
							that.longitude = res.longitude

							that.listdata.forEach(item => {
								item.icon = that.fileURL + item.icon

								// 距离计算公式
								var radweidu1 = this.Rad(res.latitude);
								var radweidu2 = this.Rad(item.address.lat);
								var a = radweidu1 - radweidu2;
								var b = that.Rad(res.longitude) - that.Rad(item.address.lng);
								// console.log(b);
								var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
									Math.cos(radweidu1) * Math.cos(radweidu2) * Math
									.pow(Math.sin(b / 2), 2)));
								// console.log(s);
								s = s * 6378.137;
								s = Math.round(s * 10000) / 10000;
								s = s.toFixed(1) //保留两位小数
								// console.log('经纬度计算的距离:' + s)
								item.distance = parseFloat(s)
							})
							
							// 按距离排序
							that.listdata.sort(function(a, b) {
								if (a.distance > b.distance) {
									return 1;
								} else if (a.distance < b.distance) {
									return -1
								} else {
									return 0;
								}
							})
							// 获取网点到用户的距离，根据经纬度判断直线距离
							let markers = []
							// for (var i = 0; i < arr.length; i++) {
							// 	markers = markers.concat({
							// 		id: arr[i].id,
							// 		latitude: arr[i].latitude, //纬度
							// 		longitude: arr[i].longitude, //经度
							// 		callout: { //自定义标记点上方的气泡窗口 点击有效
							// 			content: arr[i].name, //文本
							// 			color: '#ffffff', //文字颜色
							// 			fontSize: 10, //文本大小
							// 			borderRadius: 2, //边框圆角
							// 			bgColor: '#00c16f', //背景颜色
							// 			display: 'ALWAYS', //常显
							// 			padding:6
							// 		},
							// 	})
							// }

							arrdata.forEach(item => {
								let obj = {}
								obj.latitude = item.address.lat
								obj.longitude = item.address.lng
								obj.iconPath = item.icon
								obj.id = item.id
								obj.borderRadius = 12
								obj.width = 20 //宽
								obj.height = 30 //高
								obj.callout = { //自定义标记点上方的气泡窗口 点击有效
									content: item.name, //文本
									color: '#ffffff', //文字颜色
									fontSize: 10, //文本大小
									borderRadius: 2, //边框圆角
									bgColor: '#00c16f', //背景颜色
									display: 'ALWAYS', //常显
									padding: 6
								},
								markers.push(obj)
							})
							that.markers = markers
							that.latitude = that.markers[0].latitude
							that.longitude = that.markers[0].longitude
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.customCallout {
		box-sizing: border-box;
		background-color: #fff;
		background: #FFFFFF;
		box-shadow: 0px 4rpx 16px 0px rgba(189, 191, 193, 0.4);
		border-radius: 4rpx;
		display: inline-flex;
		padding: 6rpx 24rpx;
		justify-content: center;
		align-items: center;
		color: #2A7BE2;
	}

	.tips {
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 16rpx 30rpx;
		border-radius: 8rpx;
		position: relative;
		transition: all 0.3s linear;
		border: 1px solid #fff;
		background-color: #fdf6ec;
		border-color: #fcbd71;
		font-size: 26rpx;
		text-align: left;
		color: #606266;
		margin: 30rpx 30rpx 0 30rpx;
	}

	// 历史成交价格
	.historyPrice {
		.item {
			margin-bottom: 30rpx;
			padding-bottom: 30rpx;

			.title {
				margin: 20rpx 0;
				display: flex;
				font-size: 32rpx;
			}

			.bd {
				display: none;
			}

			&.on .bd {
				display: block;
			}
		}
	}

	.button-group-list {
		// margin-top: 30rpx;
		padding: 30rpx;
		padding-bottom: 0;
		display: flex;
		align-items: center;

		.g-button {
			flex: 1;
			text-align: center;
			background-color: #fff;
			box-shadow: $u-color-border-shadow;
			display: flex;
			justify-content: center;
		}
	}

	.u-btn {
		font-size: 30rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		border-radius: 10rpx;
	}
	
	//第一种样式
	// .list-box {
	// 	display: flex;
	// 	align-items: center;
	// 	padding: 30rpx;
	// 	background-color: #fff;
	// 	border-bottom: 1rpx solid #efefef;
		
	// 	.list-l{
	// 		width: 200rpx;
	// 		height: 190rpx;
	// 		position:relative;
	// 		border-radius: 5px;
	// 		overflow: hidden;
	// 		.tag{
	// 			position: absolute;
	// 			top: 0;
	// 			left: 3px;
	// 			.tag-item{
	// 				display: block;
	// 			}
	// 		}
	// 	}
	
	// 	.list-r {
	// 		margin-left: 30rpx;
	// 		display: flex;
	// 		flex-direction: column;
	// 		justify-content: space-between;
	// 		align-items: baseline;
	// 		height: 200rpx;
	// 		flex: 1;
	// 	}
	
	// 	.title {
	// 		font-size: 34rpx;
	// 		display: flex;
	// 		align-items: center;
	
	// 	}
	
	// 	.title-2 {
	// 		font-size: 25rpx;
	// 		color: #9fa3b0;
	// 	}
	// }
	
	// 第二种样式
	.list-box {
		display: flex;
		align-items: center;
		padding: 30rpx;
		background-color: #fff;
		border-radius: 24rpx;
		box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
		padding-right: 0;
		
		.list-l {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			align-items: baseline;
			flex: 1;
			border-right: 1rpx solid #efefef;
			padding-right: 10rpx;
			line-height: 1.4em;
		}
		
		.list-r{
			width: 200rpx;
			position:relative;
			border-radius: 5px;
			overflow: hidden;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			.distance{
				.d1{
					font-size: 24rpx;
					color: $u-tips-color;
				}
				.d2{
					font-size: 40rpx;
				}
				.d3{
					font-size: 24rpx;
				}
			}
			.btn{
				display: flex;
				align-items: center;
				justify-content: center;
				margin-top: 30rpx;
				.item{
					width: 65rpx;
					height: 65rpx;
					background: #e7fdff;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 60rpx;
					margin: 0rpx 10rpx;
				}
			}
		}

		.title {
			font-size: 34rpx;
			display: flex;
			align-items: center;

		}

		.title-2 {
			font-size: 25rpx;
			color: #9fa3b0;
		}
	}
	
	

	.search-box {
		background-color: #fff;
	}
	
	.loading-page{
		display: flex;
		align-items: center;
		justify-content: center;
		height: 200px;
	}
	
	// 回收指南
	.guide {
		.list-con {
			display: flex;
			padding: 30rpx 0;
	
			.item {
				flex: 1;
				text-align: center;
	
				.icon {
					width: 68rpx;
					height: 68rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					margin: 0 auto 10rpx;
					border-radius: 50%;
					background: #ffe8a8;
	
					.iconfont {
						color: #af9652;
					}
				}
	
				.arrow-ico {
					color: #666;
				}
	
				.name {
					font-size: 26rpx;
					margin-top: 10rpx;
				}
	
				.iconfont {
					// color: #fff;
				}
			}
		}
	
		.u-btn {
			font-size: 30rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			border-radius: 60rpx;
		}
	}
</style>